package com.lovo.ssc.controller;



import com.lovo.dto.CommodityDto;
import com.lovo.remoteservice.ICommodityService;
import com.lovo.ssc.entity.Shelves;
import com.lovo.ssc.entity.SysGoods;
import com.lovo.ssc.service.IShelvesService;
import com.lovo.ssc.service.ISysGoods2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/shelves")
@Api(tags = "上架/下架 丁宦中 003")
public class ShelvesController {
    @Autowired
    IShelvesService service;
    @Autowired
    ICommodityService commodityService;
    @Autowired
    ISysGoods2Service sysGoods2Service;

    /**
     * 添加库存商品状态为已上架的商品到上架表
     *
     * @param shelves 商品对象
     */
    @GetMapping("add")
    @ApiOperation(value = "上架商品")
    public void add(Shelves shelves) {
        service.add(shelves);
        service.upGoodsStatue(shelves.getGoodscode(), "2");
        sysGoods2Service.outStorage(shelves.getGoodscode(), shelves.getGoodsbatch(),
                shelves.getNumber());
    }

//    @PostMapping("update")
//    @ApiOperation(value = "根据编号，修改库存销售价")
//    public void update(String code ,float price){
//        service.update(code,price);
//       SysGoods goods = service.findGoodsByCode(code);
//        service.upGoodsStatue(goods.getBatch(),"1");
//    }

    @GetMapping("findGoodsBatch")
    @ApiOperation(value = "查询库存")
    public SysGoods findGoodsBatch(String code) {

        return service.findGoodsByCode(code);
    }

    @GetMapping("upGoodsStatue")
    @ApiOperation(value = "下架商品")
    public String upGoodsStatue(String code) {
        service.upGoodsStatue(code, "0");
        return "ok";
    }

    @GetMapping("del")
    @ApiOperation(value = "删除上架商品")
    public String del(String code) {
        SysGoods goods = service.findGoodsByCode(code);
        if (goods.getState().equals("0")) {
            service.delShelves(code);
            return "ok";
        }
        return "no";
    }

    @GetMapping("findAll")
    @ApiOperation(value = "查询所有批次库存")
    public List<SysGoods> findAll() {
        return service.findAll();
    }

    @GetMapping("findAllShelves")
    @ApiOperation(value = "查询所有上架商品")
    public List<Shelves> findAllShelves() {
        return service.findAllShelves();
    }

    //    @GetMapping("updateGoodsNum")
//    @ApiOperation(value = "根据批次，修改批次库存数量")
//    public String updateGoodsNum(String batch,int num){
//        SysGoods goods = this.findGoodsBatch(batch);
//        int x  =goods.getStockNum()-num;
//        service.updateGoodsNum(batch, x);
//        return "ok";
//    }
    @PostMapping("commodity")
    @ApiOperation(value = "远程调用前端接口，添加上架商品")
    public void commodity() {
        List<Shelves> list = findAllShelves();
        List<CommodityDto> dtoList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CommodityDto commodityDto = new CommodityDto();
            commodityDto.setCommoditycode(list.get(i).getGoodscode());
            commodityDto.setCommoditybatch(list.get(i).getGoodsbatch());
            commodityDto.setCommodityname(list.get(i).getGoodsname());
            commodityDto.setNorms(list.get(i).getGoodsspecs());
            commodityDto.setNumber(list.get(i).getNumber());
            commodityDto.setPrice(list.get(i).getGoodspricce());
            commodityDto.setState(list.get(i).getGoodsstate());
            commodityDto.setType(list.get(i).getGoodstype());
            dtoList.add(commodityDto);
        }
        commodityService.findAllCommodityBySscSys(dtoList);
    }
    @GetMapping("findByItem")
    public List<SysGoods> findByItem(String name,String state){
        return service.findByItem(name, state);
    }

//    public void findAllCommodityBySscSys(List<CommodityDto> commodityDtoList){
//
//    }
}

